# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import sys
import os
from PIL import Image

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox, QFileDialog
from mainWindow import Ui_Form


# PIL显示图片的RGB值
class UserWindow(QMainWindow, Ui_Form):
    def __init__(self):
        super(UserWindow, self).__init__()
        self.setupUi(self)
        self.pic = None
        self.RBtn_rgb565.setChecked(True)

        self.init()

    def init(self):
        self.Btn_open.clicked.connect(self.select_img)
        self.Btn_start.clicked.connect(self.img_convert)

    def img_convert(self):
        if self.pic is None:
            QMessageBox.warning(self, "提示", "未选择图片")
        else:
            w = self.pic.width
            h = self.pic.height
            size = str(w) + "x" + str(h)
            f = open("res.txt", "w")
            if self.RBtn_rgb888.isChecked():
                f.write("const uint32_t img"+size+"[]["+str(h)+"] = {\n")
            else:
                f.write("const uint16_t img"+size+"[]["+str(h)+"] = {\n")
            try:
                for i in range(self.pic.height):
                    s = "{"
                    for j in range(self.pic.width):
                        (r, g, b) = self.pic.getpixel((j, i))
                        if self.RBtn_rgb888.isChecked():
                            color_888 = ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF)
                            s += "0x{:04X},".format(color_888)
                        else:
                            color_565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
                            color_565 = ((color_565 & 0xFF00) >> 8) | ((color_565 & 0xFF) << 8)
                            s += "0x{:04X},".format(color_565)
                    s += "},\n"
                    f.write(s)
                f.write("}; \r\n")
                f.close()
                QMessageBox.information(self, "提示", "转换完成，结果保存")
            except Exception as e:
                print(e)

    def open_folder(self):
        path = "start explorer" + " " + self.path
        os.system(path)

    def select_img(self):
        # img_name, img_type = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;*.jpeg;;All Files(*)")
        img_name, img_type = QFileDialog.getOpenFileName(self, "打开图片", "", "All Files(*)")
        print(img_name)
        img = QtGui.QPixmap(img_name).scaled(self.label_Img.width(), self.label_Img.height())
        self.pic = Image.open(img_name)
        self.label_Img.setPixmap(img)


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    app = QApplication(sys.argv)
    window = UserWindow()
    window.show()
    sys.exit(app.exec())

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
